
<script setup >
import router from '../router'
import { default as http, getImageUrl } from '@/utils/request.js';
import { ref ,reactive} from 'vue'
const goodsLists = reactive([])

const search = ref('');
// 获取商品列表
const searchList = function () {
  //最终请求地址：/user/goods/list?key=搜索关键字
  http.get(
    "/user/goods/list", {
      params: { goodsName: search.value }
    }
  ).then((res) => {
    console.log('获取到的商品列表：', res.data);
    goodsLists.splice(0);//todo 需要将原有的元素清空
    res.data.data.list.forEach(goods => {
      goodsLists.push(goods);
    });
    console.log('获取到的商品列表ss：', goodsLists);
    // goodsLists.list = res.data.data.list;  
        console.log('获取到的商品列表ss：', goodsLists.list); 
  }).catch(err => {
    console.log(err);
  });
}
//获取搜索结果
// const searchList=function(key){
//   console.log('查询的关键字为',key);
//   http.post("/user/goods/list",key).then(res=>{
//     console.log("根据关键字查询到的结果是",res.data);
//   }).catch(err=>{
//     console.log(err);
//   })
// }



</script>

<template>
  <div >
    <van-nav-bar title="商品搜索" left-arrow @click-left="$router.go(-1)" />

    <van-search v-model="search" show-action placeholder="请输入搜索关键词" @search="searchList" clearable>
      <template #action>
        <div @click="searchList">搜索</div>
      </template>
    </van-search>
    <div v-for="gl in goodsLists" top="5px">
      <van-card  :price="gl.price"  :desc="gl.details" :title="gl.goodsName"
        :thumb="getImageUrl(gl.picture)"/>
    </div>
  </div>
</template>

<script>

</script>

<style  scoped>
.search {
  .searchBtn {
    background-color: #fa2209;
    color: #fff;
  }
/* 
  ::v-deep .van-search__action {
    background-color: #c21401;
    color: #fff;
    padding: 0 20px;
    border-radius: 0 5px 5px 0;
    margin-right: 10px;
  }

  ::v-deep .van-icon-arrow-left {
    color: #333;
  } */

  .title {
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
  }

  .list {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 10px;
    gap: 5%;
  }

  .list-item {
    width: 30%;
    text-align: center;
    padding: 7px;
    line-height: 15px;
    border-radius: 50px;
    background: #fff;
    font-size: 13px;
    border: 1px solid #efefef;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-bottom: 10px;
  }
}
</style>